/* $RCSFile: TypeConverterLanguageCultureList.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Helpers/TypeConverterLanguageCultureList.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Globalization;
using System.Collections.Specialized;
using System.ComponentModel;

namespace AddonNice.Helpers
{
	/// <summary>
	/// LanguageCultureList converter.
	/// </summary>
	public class TypeConverterLanguageCultureCollection : TypeConverter
	{
		/// <summary>
		/// Overrides the CanConvertFrom method of TypeConverter.
		/// The ITypeDescriptorContext interface provides the context for the
		/// conversion. Typically this interface is used at design time to 
		/// provide information about the design-time container.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="sourceType"></param>
		/// <returns></returns>
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(string)) 
			{
				return true;
			}
			if (sourceType == typeof(LanguageCultureCollection)) 
			{
				return true;
			}
			return base.CanConvertFrom(context, sourceType);
		}

		/// <summary>
		/// Overrides the CanConverTo method of TypeConverter.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="sourceType"></param>
		/// <returns></returns>
		public override bool CanConvertTo(ITypeDescriptorContext context, Type sourceType) 
		{
			if (sourceType == typeof(string)) 
			{
				return true;
			}
			if (sourceType == typeof(LanguageCultureCollection)) 
			{
				return true;
			}
			return base.CanConvertTo(context, sourceType);
		}

		private readonly char[] itemsSeparator = {';'};
		private readonly char[] keyValueSeparator = {'='};

		private LanguageCultureCollection GetLanguagesCultureList(string keyValueList)
		{
			//Trim last separator if found
			keyValueList                            =   keyValueList.TrimEnd(itemsSeparator);
			string[] nameValues                     =   keyValueList.Split(itemsSeparator);
			LanguageCultureCollection nameValueColl =   new LanguageCultureCollection();
			foreach (string nameValue in nameValues)
			{
				if (nameValue != string.Empty)
				{
					string[] arrNameValue = nameValue.Split(keyValueSeparator);

					CultureInfo language;
					CultureInfo culture;
				
					language = new CultureInfo(arrNameValue[0]);

					if (arrNameValue.Length == 2)
					{
						culture = new CultureInfo(arrNameValue[1]);
						nameValueColl.Add(new LanguageCultureItem(language, culture));
					}
					if (arrNameValue.Length == 1)
					{
						culture = new CultureInfo(arrNameValue[0]);
						if (culture.IsNeutralCulture) //we cannot allow this
							culture = CultureInfo.CreateSpecificCulture(culture.Name); //creates a specific culture
						nameValueColl.Add(new LanguageCultureItem(language, culture));
					}
				}
			}
			return nameValueColl;		
		}

		/// <summary>
		/// Overrides the ConvertFrom method of TypeConverter.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="culture"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
		{ 
			if (value is string) 
			{
				return GetLanguagesCultureList((string) value);
			}
			if (value is LanguageCultureCollection) 
			{
				return ((LanguageCultureCollection)value).ToString();
			}
			return base.ConvertFrom(context, culture, value);
		}

		/// <summary>
		/// Overrides the ConvertTo method of TypeConverter.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="culture"></param>
		/// <param name="value"></param>
		/// <param name="destinationType"></param>
		/// <returns></returns>
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
		{  
			if (destinationType == typeof(string)) 
			{
				return ((LanguageCultureCollection)value).ToString();
			}
			if (destinationType == typeof(LanguageCultureCollection)) 
			{
				return GetLanguagesCultureList((string) value);
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}
	}
}
